home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Collection of Tools & Utilities
/
Collection of Tools and Utilities.iso
/
c
/
cuj9205.zip
/
1005087A
< prev
next >
Wrap
Text File
|
1992-06-02
|
1KB
|
75 lines
Listing 2
//
// rational.cpp
//
#include "mylib.h"
#include "rational.h"
rational rational::operator-()
{
rational result(*this);
result.num = -result.num;
return result;
}
rational rational::operator++(int)
{
rational result(*this);
*this += 1;
return result;
}
rational rational::operator--(int)
{
rational result(*this);
*this -= 1;
return result;
}
rational &rational::operator+=(rational r)
{
num = num * r.denom + r.num * denom;
denom *= r.denom;
simplify();
return *this;
}
rational &rational::operator-=(rational r)
{
num = num * r.denom - r.num * denom;
denom *= r.denom;
simplify();
return *this;
}
rational &rational::operator*=(rational r)
{
num *= r.num;
denom *= r.denom;
simplify();
return *this;
}
rational &rational::operator/=(rational r)
{
num *= r.denom;
denom *= r.num;
simplify();
return *this;
}
void rational::put(FILE *f)
{
fprintf(f, "(%ld/%ld)", num, denom);
}
void rational::simplify()
{
long x = gcd(num, denom);
num /= x;
denom /= x;
}